<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\validate\MoneyList as ValObj;
use app\common\model\mysql\MoneyList as ModelObj;
use think\Exception;
use think\facade\Log;

class MoneyList extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * @param string $openid
     * @return int|mixed
     * @throws Exception
     */
    public function getMoneyListByCount(string $openid = '')
    {
        try {
            $money = $this->modelObj->getMoneyListByCount($openid);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $money['money'] ?? 0;
    }

    public function setMoneyOpenid(array $data = [])
    {
        //查询可用金额
        $money = (new Money())->getUserOpenidByMoneyCount($data['openid']);
        if (floatval($data['money']) > $money) {
            throw new Exception("提现金额太大");
        }
        //信息入库
        $this->isValidateAdd($data, 'addM');
        try {
            $result = $this->modelObj->insertAdd($data);
        } catch (\Exception $e) {
            throw new Exception("添加失败");
        }
        return $result;
    }

    /**
     * @param array $where
     * @param int $limit
     * @param int $page
     * @return array
     * @throws Exception
     */
    public function getMoneyListByList(array $where = [], int $limit = 10, int $page = 1)
    {
        try {
            $result = $this->modelObj->getMoneyListByList($where, $limit, $page);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $result;
    }
}